package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.config.GetUser;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.common.excpetion.CustException;
import com.heima.model.behavior.dtos.CollectionBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service("ApCollectionBehaviorService")
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    private GetUser getUser;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 收藏 取消收藏
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectBehavior(CollectionBehaviorDTO dto) {
        //根据当前线程中的用户信息查询MongoDB中的用户信息实体类
        ApUser user = getUser.getApUser();

        // 用户还未登录 不允许收藏操作
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        //用户执行的是收藏操作
        if (dto.getOperation() == 0) {
            // 根据实体Id查询
            ApCollection collection = mongoTemplate.findOne(Query.query(
                            Criteria
                                    .where("entryId").is(apBehaviorEntry.getId())
                                    .and("articleId").is(dto.getArticleId()))
                    , ApCollection.class);
            //用户已经收藏过
            if (collection != null)
                return ResponseResult.okResult();

            // 添加用户数据
            ApCollection apCollection = new ApCollection();
            apCollection.setArticleId(dto.getArticleId());
            apCollection.setEntryId(String.valueOf(apBehaviorEntry.getId()));
            apCollection.setCollectionTime(new Date());
            apCollection.setType(dto.getType());
            mongoTemplate.save(apCollection);
        } else {
            //用户执行的是取消收藏操作
            mongoTemplate.remove(Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId())), ApCollection.class);
        }
        // 根据用户执行的操作向MQ发送收藏情况消息
        {
            UpdateArticleMess articleMess = new UpdateArticleMess();
            articleMess.setType(UpdateArticleMess.UpdateArticleType.COLLECTION);
            //如果是0 ->点赞操作 ->设置增加属性为1
            articleMess.setAdd(dto.getOperation() == 0 ? 1 : -1);
            articleMess.setArticleId(dto.getArticleId());
            //发送消息
            rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(articleMess));
        }

        return ResponseResult.okResult();
    }
}
